﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using olierFrm.GeneralCache;
using System.Linq.Expressions;

namespace olierFrm.DataBase.FrameWork.Plugin
{
    public class ReceiptsConfig
    {
        public const string ReceiptsConfigCachePrefix = "ReceiptsConfig=";
        private Dictionary<string, Dictionary<string, string>> dsc;
        public string IniScript
        {
            get;
            private set;
        }
        public ReceiptsConfig(string module)
        {

            string CacheKey = "ReceiptsConfig=" + module;
            WF_Receipts StdReceipts = null;
            if (!Cache.KeyExists(CacheKey))
            {
                FrameWorkEntity sdc = new FrameWorkEntity(ConnectionConfig.StaEntityConnectionStr);
                var rcs = sdc.WF_Receipts.Where(s => s.sModuleID == module);

                if (rcs.Any())
                {
                    StdReceipts = rcs.First();
                    Cache.Add(CacheKey, StdReceipts);
                }
            }
            else
            {
                StdReceipts = (WF_Receipts)Cache.Get(CacheKey);
            }
            this.IniScript = ((StdReceipts == null) ? "" : StdReceipts.sConfig);
        }
        public Dictionary<string, string> GetSection(string section)
        {
            if (this.dsc == null)
            {
                this.dsc = SmartINI.LoadIni(this.IniScript);
            }
            Dictionary<string, string> result;
            if (this.dsc.ContainsKey(section))
            {
                result = this.dsc[section];
            }
            else
            {
                result = null;
            }
            return result;
        }
        public string GetPrivateProfileString(string section, string key)
        {
            Dictionary<string, string> sections = this.GetSection(section);
            string result;
            if (sections == null || !sections.ContainsKey(key))
            {
                result = null;
            }
            else
            {
                result = sections[key];
            }
            return result;
        }
    }
}
